from datetime import datetime
from importlib import reload

import os,sys
project_root = os.path.dirname(os.path.dirname(os.path.abspath()))
sys.path.append(project_root)

from htbacktrader_portfoliostrategy import BacktestingEngine
from htbacktrader_core.trader.constant import Interval
from htbacktrader_core.trader.optimize import OptimizationSetting

import htbacktrader_portfoliostrategy.strategies.pair_trading_strategy as stg
from htbacktrader_portfoliostrategy.strategies.pair_trading_strategy import PairTradingStrategy



engine = BacktestingEngine()
engine.set_parameters(
    vt_symbols=["y888.DCE", "p888.DCE"],
    interval=Interval.MINUTE,
    start=datetime(2019, 1, 1),
    end=datetime(2020, 4, 30),
    rates={
        "y888.DCE": 0/10000,
        "p888.DCE": 0/10000
    },
    slippages={
        "y888.DCE": 0,
        "p888.DCE": 0
    },
    sizes={
        "y888.DCE": 10,
        "p888.DCE": 10
    },
    priceticks={
        "y888.DCE": 1,
        "p888.DCE": 1
    },
    capital=1_000_000,
)

setting = {
    "boll_window": 20,
    "boll_dev": 1,
}
engine.add_strategy(PairTradingStrategy, setting)

engine.load_data()
engine.run_backtesting()
df = engine.calculate_result()
engine.calculate_statistics()
# engine.show_chart()